// pages/99/99.js
const { Tab, extend } = require('../../dist/index');
var app = getApp();
Page(extend({}, Tab, {

  /**
   * 页面的初始数据
   */
  data: {
    sort_method: {
      list: [{
        id: 'hot',
        name: '销量'
      }, {
        id: 'new',
        name: '最新'
      }, {
        id: 'low',
        name: '券额'
      }, {
        id: 'price',
        name: '价格'
      }],
      selectedId: 'hot'
    },
    couponList: [],
    selectCategory: "1",
    picWidth: wx.getSystemInfoSync().windowWidth,
    pageIndex: 1,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcate();
    this.getMoreCouponList();
    this.setData({ is_kouling: app.globalData.is_kouling})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //取分类
  getcate: function () {
    var that = this
    wx.request({
      url: app.globalData.apiurl,
      data: { a: "jx_cate" },
      success: function (res) {
        if (res.data.data.result.cate_list != undefined && res.data.data.result.cate_list.length >0){
          res.data.data.result.cate_list.forEach(function(list){
            list.name=list.show_name
          })
        that.setData({
          cate_method: {
            list: res.data.data.result.cate_list,
            selectedId: "1",
            scroll: true,
            height: 42,
          }
        })
        }
      }
    })
  },
  //取商品
  getMoreCouponList: function () {
    wx.showLoading({
      title: '数据加载中',
    })
    var that = this
    wx.request({
      url: app.globalData.apiurl,
      data: {
        "a": "jx",
        "p": that.data.pageIndex,
        "cid": that.data.selectCategory
      },
      method: "GET",
      success: function (resRequest) {
       // console.log(resRequest.data);
        if (resRequest.statusCode === 200) {
          if (resRequest.data.data.result != null && resRequest.data.data.result.length > 0) {


            /*
            if (that.data.sorts == "ratio") {
               resRequest.data.data.sort(that.compare('yongjin','jx'))
               }else if(that.data.sorts == "price") {
              resRequest.data.data.sort(that.compare('price','sx'))
            } */
            that.setData({
              couponList: that.data.couponList.concat(resRequest.data.data.result),
            })
            wx.hideLoading()
          }
          else {
           wx.showToast({
             title: '无更多商品了',
           })
          }
        }
      }
    })
  },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    if (componentId === "cate_method") {
      this.setData({
        selectCategory: selectedId
      })
    }
    if (componentId === "sort_method") {
      this.setData({
        sorts: selectedId
      })
    }
    this.setData({
      [`${componentId}.selectedId`]: selectedId,
      isLoading: true,
      couponList: [],
      pageIndex: 1,
    });
    this.getMoreCouponList()
  },
  onPullDownRefresh: function () {
    this.setData({
      couponList: [],
      isLoading: true,
      pageIndex: 1
    })
    wx.stopPullDownRefresh()
    this.getMoreCouponList()
  },
  onReachBottom: function () {
    this.setData({
      isLoading: true,
      pageIndex: this.data.pageIndex + 1
    })
    this.getMoreCouponList()
  },

}))